﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;

namespace P03
{
    public class A07 : IPDAChild
    {
        String Result = "";
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P03A07 P = (P03A07)handler.SerialApi.Deserialize(typeof(P03A07), parameter, CR.SerialType);

            try
            {
                if (!FillOrder(handler,P))
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = Result;
                    return;
                }

                if (!DistAccQty(handler, P))
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = Result;
                    return;
                }
                string strGUID = System.Guid.NewGuid().ToString();
                SqlParameter[] sps = new SqlParameter[10];
                sps[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
                sps[1] = new SqlParameter("@QTY", SqlDbType.Decimal, 2);
                sps[2] = new SqlParameter("@QTY_AuxSuttle", SqlDbType.Decimal, 2);
                sps[3] = new SqlParameter("@QTY_AccSuttle", SqlDbType.Decimal, 2);
                sps[4] = new SqlParameter("@Emp", SqlDbType.NChar, 50);
                sps[5] = new SqlParameter("@Send", SqlDbType.NChar, 50);
                sps[6] = new SqlParameter("@Batch", SqlDbType.NChar, 50);
                sps[7] = new SqlParameter("@Line", SqlDbType.Int);
                sps[8] = new SqlParameter("@Stamp", SqlDbType.NChar, 50);
                sps[9] = new SqlParameter("@OutTime", SqlDbType.NChar, 50);
                sps[0].Value = P.FID;
                sps[1].Value = P.Qty+1;
                sps[2].Value = P.Qty;
                sps[3].Value = P.Qty;
                sps[4].Value = "";
                sps[5].Value = user.UserName;
                sps[6].Value = "";
                sps[7].Value = 1;
                sps[8].Value = strGUID;
                sps[9].Value = DateTime.Now;

                String SQL = "Update KEW_SalesInfo set FDecimal=1.00,FTime1=@OutTime where FID=@FID ";
                SQL += "EXEC KEW_SALES_SAVE2 @FID,@QTY,@QTY_AuxSuttle,@QTY_AccSuttle,@Emp,@Send,@Batch,@Line,@Stamp,@OutTime";
                Recordset rs = handler.Data.ExeProcedure(P.AccountNo, SQL, sps);

                if (rs.Enable)
                {
                    CR.Result = EnumResult.Normal;
                    CR.Message = "出厂信息计量成功";
                    handler.Message(MessageType.Note, String.Format("操作员{0}对单据{1}进行了出厂信息计量", user.UserName, P.FID));
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = rs.Message;

                }

                handler.Message(MessageType.Note, CR.Message);
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("发生错误:{0}", ex.Message));
            }
        }

        public bool FillOrder(IPDA handler, P03A07 P)
        {
            String SQL = "";
            FillOrderInfor fo = new FillOrderInfor();

            SqlParameter[] sps = new SqlParameter[1];
            sps[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
            sps[0].Value = P.FID;

            SQL = "SELECT A.FID FID,A.FEntryID EntryID,A.FID_SRC FID_SRC,B.FBase FBase ";
            SQL += "FROM KEW_SalesInfoEntry A ";
            SQL += "LEFT JOIN KEW_SalesInfo B ON A.FID=B.FID ";
            SQL += "WHERE A.FID=@FID AND A.FID_SRC>0 ORDER BY A.FIndex ";

            Recordset rs = handler.Data.ExeProcedure(P.AccountNo, SQL, sps);
            if (!rs.Enable)
            {
                Result = rs.Message;
                return rs.Enable;
            }

            foreach (DataRow row in rs.Data.Tables[0].Rows)
            {
                SqlParameter[] sps1 = new SqlParameter[3];
                sps1[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
                sps1[1] = new SqlParameter("@FID_SRC", SqlDbType.NChar, 50);
                sps1[2] = new SqlParameter("@FBase", SqlDbType.NChar, 50);
                sps1[0].Value = P.FID;
                sps1[1].Value = row["FID_SRC"];
                sps1[2].Value = row["FBase"];

                SQL = "EXEC KEW_SALES_ACCOUNT_GET0 @FID_SRC,@FBase,@FID";
                Recordset Rs = handler.Data.ExeProcedure(P.AccountNo, SQL, sps1);

                if (!Rs.Enable)
                {
                    Result = Rs.Message;
                    return false;
                }

                fo.BalAmount = Common.CDbl(Rs.Data.Tables[0].Rows[0]["BalAmount"].ToString());
                fo.FPrice = Common.CDbl(Rs.Data.Tables[0].Rows[0]["FPrice"].ToString());
                if (fo.FPrice > 0)
                {
                    fo.BalQty = fo.BalAmount / fo.FPrice;
                }
                fo.FPolicy = Rs.Data.Tables[0].Rows[0]["FPolicy"].ToString();
                fo.FTax = Common.CDbl(Rs.Data.Tables[0].Rows[0]["FTax"].ToString());

                SqlParameter[] sps2 = new SqlParameter[7];
                sps2[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
                sps2[1] = new SqlParameter("@FID_SRC", SqlDbType.NChar, 50);
                sps2[2] = new SqlParameter("@BalAmount", SqlDbType.Decimal);
                sps2[3] = new SqlParameter("@FPrice", SqlDbType.Decimal);
                sps2[4] = new SqlParameter("@BalQty", SqlDbType.Decimal);
                sps2[5] = new SqlParameter("@FPolicy", SqlDbType.NChar, 50);
                sps2[6] = new SqlParameter("@FTax", SqlDbType.Decimal);

                sps2[0].Value = P.FID;
                sps2[1].Value = row["FID_SRC"];
                sps2[2].Value = fo.BalAmount;
                sps2[3].Value = fo.FPrice;
                sps2[4].Value = fo.BalQty;
                sps2[5].Value = fo.FPolicy;
                sps2[6].Value = fo.FTax;

                SQL = "Update KEW_SalesInfoEntry set FDecimal5=@BalQty,FDecimal6=@BalAmount,FDecimal7=@FPrice,FDecimal8=@FTax,FText13=@FPolicy,FDecimal15=@FPrice,FDecimal23=@FPrice where FID=@FID and FID_SRC=@FID_SRC";

                Recordset Rs1 = handler.Data.ExeProcedure(P.AccountNo, SQL, sps2);

                if (!Rs1.Enable)
                {
                    Result = Rs1.Message;
                    return false;
                }
            }

            return true;
        }

        public bool DistAccQty(IPDA handler, P03A07 P)
        {
            String SQL = "";
            double Qty = P.Qty;
            double SumQty = 0;
            FillOrderInfor fo = new FillOrderInfor();
            List<AccountTemp> AccountList = new List<AccountTemp>();
            SqlParameter[] sps = new SqlParameter[1];
            sps[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
            sps[0].Value = P.FID;

            SQL = "SELECT A.FEntryID EntryID,A.FDecimal5 QtyBal,A.FDecimal15 FPrice ";
            SQL += "FROM KEW_SalesInfoEntry A ";
            SQL += "LEFT JOIN KEW_SalesInfo B ON A.FID=B.FID ";
            SQL += "WHERE A.FID=@FID AND A.FID_SRC>0 ORDER BY A.FIndex ";

            Recordset rs = handler.Data.ExeProcedure(P.AccountNo, SQL, sps);
            if (!rs.Enable)
            {
                Result = rs.Message;
                return rs.Enable;
            }

            //FDecimal5 剩余数量,FDecimal6 剩余金额，FDecimal15 单价合计，FDecimal9 本次扣除数量，FDecimal10 扣除金额
            foreach (DataRow row in rs.Data.Tables[0].Rows)
            {

                AccountTemp temp = new AccountTemp();
                temp.ID = row["EntryID"].ToString();
                temp.QtyBal = Common.CDbl(row["QtyBal"].ToString());//订单余量
                temp.SumPrice = Common.CDbl(row["FPrice"].ToString());//价格
                //temp.SurQty = Common.CDbl(row["Amount"].ToString());//订单余量
                // temp.QtyBal = temp.AmountBal / temp.SumPrice;

                if (Qty > temp.QtyBal)
                {
                    temp.Qty = temp.QtyBal;
                    Qty = Qty - temp.QtyBal;
                }
                else
                {
                    temp.Qty = Qty;
                    //订单余量
                    Qty = 0;
                }
                temp.Amount = Common.CDbl((temp.Qty * temp.SumPrice).ToString());
                SumQty += temp.Qty;
                AccountList.Add(temp);
            }

            SQL = "";
            foreach (AccountTemp temp in AccountList)
            {
                SQL += String.Format("UPDATE KEW_SalesInfoEntry SET FDecimal9={0},FDecimal10={1},FDecimal18={2},FDecimal16={3} WHERE FEntryID={4}\r\n",
                    temp.Qty, temp.Amount, temp.QtyBal - temp.Qty, temp.Qty, temp.ID);
            }

            rs = handler.Data.ExeProcedure(P.AccountNo, SQL);
            if (!rs.Enable)
            {
                Result = rs.Message;
                return rs.Enable;
            }

            return true;
        }
    }

    public class AccountTemp
    {
        public string ID = "";
        public double AmountBal = 0;
        public double QtyBal = 0;
        public double SumPrice = 0;
        public double Qty = 0;
        public double Amount = 0;
        public double SurQty = 0;
    }

    public class FillOrderInfor
    {
        public double BalAmount = 0;
        public double FPrice = 0;
        public double BalQty = 0;
        public String FPolicy = "";
        public double FTax = 0;
    }

    public class P03A07
    {
        public String FID = "";
        public String AccountNo;
        public double Qty;
    }
}
